﻿using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using ConsoleApp1.Utility;

namespace Sample.ConsoleApp.CodeFist.Sqlite.DbContexts;

/// <summary>
/// MyDb2Context 工厂
/// </summary>
internal class MyDb2ContextFactory : IDesignTimeDbContextFactory<MyDb2Context>
{
    public MyDb2Context CreateDbContext(string[] args)
    {
        // 获取 sqlite 连接字符串
        var directoryHelper = new DirectoryHelper();
        string connectionString = directoryHelper.GetSqliteConnectionString();

        // 构建 DbContextOptions 对象
        var optionsBuilder = new DbContextOptionsBuilder<MyDb2Context>();
        optionsBuilder.UseSqlite(connectionString);

        // 有参构造函数使用 DbContextOptions 对象
        return new MyDb2Context(optionsBuilder.Options);
    }
}
